package com.elot.core.utils;

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import com.elot.core.enums.RoleEnum;

public class Security {

	public static boolean isAuthenticated() {
		return SecurityContextHolder.getContext()!=null 
				&& SecurityContextHolder.getContext().getAuthentication()!=null 
				&& SecurityContextHolder.getContext().getAuthentication().isAuthenticated();
	}
	
	public static boolean hasRole(RoleEnum role) {
		boolean hasRole = false;
		if (isAuthenticated()) {
			Collection<GrantedAuthority> auths = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
			if (auths!=null && auths.size()>0) {
				for(GrantedAuthority auth : auths) {
					if(role.name().equals(auth.getAuthority())) {
						hasRole = true;
						break;
					}
						
				}
			}
		}
		return hasRole;
	}
	
}
